<?php

namespace app\common\model;

use app\common\OrderStatus;
use think\Model;

class Order extends Model
{
    //
    function create_order(
        $community_id,
        $seller_id,
        Users $user,
        $address,
        $goods,
        $ziti_type,
        $memo
    )
    {
        if (isset($address->user_name)) {
            $order['user_name'] = $address->user_name;
            $order['telephone'] = $address->telephone;
            $order['address'] = $address->address;
        }
        $order_id = date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        $order['user_id'] = $user->id;
        $order['community_id'] = $community_id;
        $order['order_id'] = $order_id;
        $order['seller_id'] = $seller_id;
        $order['memo'] = $memo;
        $order['ziti_type'] = isset($ziti_type) ? $ziti_type : 0;
        $order['created_time'] = date("Y-m-d H:i:s");
        /** 单品信息 */
        $money = 0;
        foreach ($goods as $v) {
            /** @var \app\common\model\Goods $good */
            $good = $v['goods'];
            $count = $v['count'];
            if (empty($good)) {
                continue;
            }
            $bar_code = $good->bar_code;
            $array['goods_id'] = $good->id;
            $array['price'] = $good->price;
            $array['count'] = $count;
//            $array['actual_count'] = $count;//实际重量
            $array['weight_total'] = $good->weight * $count;
//            $array['actual_weight_total'] = $good->weight * $count;//实际重量
            $array['goods_name'] = $good->getData("goods_name");
            $array['goods_image'] = $good->image;
            $array['goods_info'] = $good;
            $array['total'] = $good->price * $count;
            $array['user_id'] = $user->id;
            $money += $array['total'];
            $array['order_id'] = $order_id;
            OrderItem::create($array);
        }
        $order['total'] = $money;
        $order['order_id'] = $order_id;
        $this->save($order);
        /** 待支付 */
        OrderStatus::daizhifu($order_id);
        return $this;
    }

    /**
     * 根据订单id获取订单详情
     * @param $order_id
     * @return array|false|\PDOStatement|string|Model
     */
    static function get_one_order_($order_id)
    {
        $s = new self();
        $data = $s->where('order_id',$order_id)->find();
        return $data;
    }

    /**
     * 获取订单信息和每条订单的商家信息
     * @param $param
     * @return \think\Paginator
     */
    static function list_($param = [], $cond = [])
    {
        $s = new self();
        #搜索订单号,用户名和电话号
        if (isset($param['search_']) && !empty($param['search_'])) {
            $search_ = $param['search_'];
            $cond["shop_order.order_id|shop_order.user_name|shop_order.telephone"] = ["LIKE", "%$search_%"];
        }
        #筛选订单状态
        if (isset($param['order_status_id']) && !empty($param['order_status_id'])) {
            $cond['order_status_id'] = array('in', $param['order_status_id']);
        }
        #数据是否分页
        $data = $s
            ->field('shop_order.*,shop_sellers.seller_name,shop_sellers.seller_address')
            ->join('shop_sellers', 'shop_order.seller_id=shop_sellers.id');
        if (isset($param['export']) && $param['export'] == 1) {
            $data = $data->where($cond)->select();
        } else {
            $data = $data->where($cond)->paginate(30, false, ['query' => request()->param()]);
        }
        return $data;
    }

    /**
     * 根据订单号获取订单详情
     * @param $order_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    static function order_details($order_id)
    {
        $s = new self();
        $data = $s
            ->field('shop_order_item.*')
            ->join('shop_order_item','shop_order.order_id = shop_order_item.order_id')
            ->where('shop_order.order_id',$order_id)
            ->select();
        return $data;
    }

}
